/**
 * Created with CLion.
 * Description: 
 * User: loong
 * Date: 2021-03-23
 * Time: 18:04
 */
#ifndef CODSEEGMENT_DIAGRAMBASEITEM_H
#define CODSEEGMENT_DIAGRAMBASEITEM_H

#include <QtWidgets>
#include "PropertyDialog.h"
#include "DiagramBasePrivateText.h"

class Arrow;
class DiagramBasePrivateText;
class DiagramBaseItem
        : public QGraphicsPolygonItem
{
Q_GADGET
public:
    /*[NOTE] 2021/3/23 19:02 loong: 每个继承该类的子类都要重新定义Type，且各个子类的Type不相同 */
    enum { Type = UserType + 15 };
    enum ItemFlag { CanMove = 0x01, CanRotate = 0x02, CanFlip = 0x04, CanResize = 0x08, CanInsertRemovePoints = 0x10};
    Q_DECLARE_FLAGS(ItemFlags, ItemFlag)

    enum PlaceType{ Horizontal, Vertical };
    Q_ENUM(PlaceType)

    enum ItemData{ IDData,ParentIDData,TitleData,IsRootData,PlaceTypeData,PosXData,PosYData,WidthData,HeightData,UnitRectData} ;
    Q_ENUM(ItemData)

    explicit DiagramBaseItem(const QString &item_title, bool is_root = false, QGraphicsItem *parent = nullptr);

    ~DiagramBaseItem() override;

    /*[0]: Getter and Setter */
    PlaceType GetPlaceType() const
    { return place_type_; }

    void SetPlaceType(PlaceType placeType)
    { place_type_ = placeType; }

    bool IsRoot() const
    { return is_root_; }

    void SetIsRoot(bool isRoot)
    { is_root_ = isRoot; }

    /**
     * @brief 获取逻辑输出点
     * @return
     */
    QPointF GetOutPoint()
    { return mapToScene(out_point_); }

    /**
     * @brief 获取逻辑输入点
     * @return
     */
    QPointF GetInPoint()
    {return mapToScene(in_point_); }



    /*[1]: Arrow */
/*    void AddArrow(Arrow *arrow);

    void RemoveArrow(Arrow *arrow);

    void RemoveArrows();*/

    /*[2]: Child Item and Parent Item */
//    void AddParent(DiagramBaseItem * parent)
//    { parent_items_ << parent ; }

    void AddChild(DiagramBaseItem * child)
    { children_items_ << child; }

    QList<DiagramBaseItem *> ChildItems()
    {return children_items_;};

    void SetItemPos(QPointF pos);

    void SetItemSize(QSizeF size);

    /*[3]: implement QGraphicsPolygonItem function */

    int type() const override
    { return Type; }

    QRectF boundingRect() const override;

    void RotateItem();

    virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override;

private:
    void InitUI();
    void InitEvent();
    void InitProperty();
protected:
    virtual void hoverMoveEvent(QGraphicsSceneHoverEvent *event) override;

    virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override;

    virtual void mousePressEvent(QGraphicsSceneMouseEvent *event) override;

    virtual void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) override;

    virtual QVariant itemChange(GraphicsItemChange change, const QVariant &value) override;

private:
    DiagramBasePrivateText *        text_item_;
    //QGraphicsTextItem *             text_item_;
    //QMenu *                         item_menu_;
    //QList<Arrow *>                  arrows_;
    QPolygonF                       item_polygon_;
    QHash<QString, QVariant>        item_properties_;
    bool                            resize_pressed_;
    QString                         item_title_;
    PlaceType                       place_type_;

    QList<DiagramBaseItem*>         children_items_;
//    QList<DiagramBaseItem*>         parent_items_;

    bool                            is_root_;
    QPointF                         in_point_;
    QPointF                         out_point_;

    void UpdateProperty();

    void UpdateConnectPoint();
};

Q_DECLARE_OPERATORS_FOR_FLAGS(DiagramBaseItem::ItemFlags)

#endif //CODSEEGMENT_DIAGRAMBASEITEM_H
